package com.r2coding.article.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.r2coding.article.vo.req.CommentReqVo;
import com.r2coding.article.vo.resp.AdminCommentRespVo;
import com.r2coding.article.vo.resp.CommentsRespVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.article.entity.CommentEntity;
import com.r2coding.article.service.CommentService;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Result;

/**
 * 评论信息表
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-13 19:34:16
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = commentService.queryPage(params);
        return Result.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") String id){
		CommentEntity comment = commentService.getById(id);
        return Result.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody CommentEntity comment){
		commentService.save(comment);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CommentEntity comment){
		commentService.updateById(comment);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody String[] ids){
		commentService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

    /**
     * 后台管理查询评论列表
     *
     * @param req 请求信息
     * @return 统一响应体
     */
    @PostMapping("/comments")
    public Result comments(@RequestBody CommentReqVo req) {
        AdminCommentRespVo resp = commentService.comments(req);
        return Result.ok(resp);
    }

    /**
     * 后台管理删除评论
     *
     * @param id 评论id
     * @return 统一响应体
     */
    @PreAuthorize("hasAuthority('comment:delete')")
    @DeleteMapping("/delete/{id}")
    public Result deleteComment(@PathVariable("id") Integer id) {
        commentService.deleteComment(id);
        return Result.ok();
    }

}
